const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        count:0,
        total:0,
        number:0,
        timest:0,
        idArray:[],
        timeList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var open=wx.getStorageSync('open');
        that.setData({
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    goBack:function(e){
      console.log(e)
      var pages = getCurrentPages();
      console.log(pages);
      wx.navigateBack();
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getIndex();
    },
    getIndex(){
        var open=wx.getStorageSync('open');
        let that=this;
        var domain=this.data.domain;
        wx.request({
          url: domain+'shopcart/index',
          data:{user_id:open.user_id,from:3},
          success:res=>{
              let data=res.data;
              console.log(res);
              var lists=data.list;
              lists.forEach(function(item,index){
                console.log(index)
               // that.setTime(item.time,index);
               if(index==0){
                   console.log(0);
                    that.data.timert = setInterval(
                        function () {
                    that.toClock();        
                    }, 1000);
            
                    that.setData({
                        time:item.time,
                        timert:that.data.timert
                    });
               }
               if(index==1){
                console.log('index=1');
                   that.data.timerst = setInterval(
                    function () {
                    that.setTime();        
                    }, 1000);
            
                    that.setData({
                        timest:item.time,
                        timerst:that.data.timerst
                    });
               }
              })
              that.setData({
                  couponImg:data.couponImg,
                  list:data.list,
                  count:data.count
              })
          }

        })
    },
    setTime(){
        let that=this;
        var  timeList=this.data.timeList;
        var timest=this.data.timest
        if(timest>0){
            timest=timest-1;          
            var minute=parseInt(timest/60);
            var second=timest%60;
            timeList[1]=minute+'分'+second+'秒';
            console.log('times'+timest);
            that.setData({
                timest:timest,
                timeList:timeList 
            })   
     
        }else{
            clearInterval(that.data.timerst)
        }
    },
    toClock(){
        let that=this;
         var  timeList=this.data.timeList;
         var time=this.data.time
        // timeList[index]=times;
         if(time>0){
            time=time-1;           
            var minute=parseInt(time/60);
            var second=time%60;
            timeList[0]=minute+'分'+second+'秒';
            console.log('time'+time);
            that.setData({
                time:time,
                timeList:timeList 
            })
         }else{
             clearInterval(that.data.timert)
         }
    },
    selectCoupon:function(e){
        console.log(e);       
        var data=this.data;
        var  list= data.list;
        var id=e.currentTarget.dataset.id;
        var number=data.number;
        var total=data.total;     
        console.log(id);
        var idArray=data.idArray;
        list.forEach(function(item,index){      
            var money=item.pay_money;    
            console.log(money);          
            if(id==item.id){
                if(item.select==1){
                    item.select=0;
                    total=parseFloat(total)-parseFloat(money);
                    number=number-1;      
                    console.log(id);
                    var ids=idArray.indexOf(id)
                    if (ids > -1) { 
                        idArray.splice(ids, 1); 
                      } 
                }else{
                    item.select=1;
                    total = parseFloat(total)+parseFloat(money);
                    number=number+1;
                   idArray.push(item.id);
                   
                }
                
            }
           
        })
 
        this.setData({
            total:total,
            number:number,
            list:list,
            idArray:idArray
   
        })

    },
    payment(){
        let that=this;
        var data=this.data;
        var open=wx.getStorageSync('open');
        console.log(open)
        var idArray=data.idArray;
        var money = data.total;
        var number = data.number;
        var domain=data.domain;
        if(money){           
            wx.request({
              url: domain+'pay/payment',
              data:{user_id:open.user_id,openid:open.openid,money:money,ids:idArray.toString(),number:number},
              success: function (res) {
                console.log(res.data);
                 //console.log('调起支付');
                 var code=res.data.code;
                 if(code==200){
                 var data=res.data.data;
                   wx.requestPayment({
                       'timeStamp': data.timeStamp,
                       'nonceStr': data.nonceStr,
                       'package': data.package,
                       'signType': 'MD5',
                       'paySign': data.paySign,
                       'success': function (res) {
                        // console.log('success');
                         wx.showToast({
                           title: '支付成功',
                           icon: 'success',
                           duration: 3000
                         });
                         setTimeout(function() {
                             //要延时执行的代码
                             wx.reLaunch({
                         
                               url: '../mine/index',
                             })
                           }, 1000) //延迟时间
                         //that.waitPayShow();
                       },
                       'fail': function (res) {
                         //console.log(res);
                       },
                       'complete': function (res) {
                        // console.log('complete');
                       }
                     });
                 }else{
           
                 }
             
               },
               fail: function (res) {
                 //console.log("fail")
               }
            })
        }else{
          console.log('payment')  
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})